<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\order\store;


use app\care\Youmen;
use app\Core\umeng\Umeng;
use app\dao\order\StoreOrderDao;
use app\model\v1\recovery\ConfigUmengMsg;
use app\Request;
use app\services\BaseServices;
use app\services\order\OtherOrderServices;
use app\services\order\StoreOrderRefundServices;
use app\services\order\StoreOrderServices;
use app\services\store\StoreUserServices;
use app\services\user\UserCardServices;
use app\services\user\UserRechargeServices;
use app\services\user\UserServices;
use think\exception\ValidateException;

/**
 * Class StoreOrderWapServices
 * @package app\services\order
 * @mixin StoreOrderDao
 */
class PushMsgServices extends BaseServices
{

    protected $title;
    /**
     * StoreOrderWapServices constructor.
     * @param StoreOrderDao $dao
     */
    public function __construct(StoreOrderDao $dao)
    {
        $this->dao = $dao;
        $this->title = "【尚百街】";
    }


    const UMENG_APPKEY = '6706575061c3b13bde9e0d7a';
    const UMENG_APPSECRET = 'jdqilvvvrs2sann1rjrhnah39b6xxtsl';

    const UMENG_VERIFY_APPKEY = '204737075';
    const UMENG_VERIFY_APPSECRET = 'M8NetK5hYkFbzNcdhFqiawyi6X1yv1oM';
    const UMENG_VERIFY_APPCODE = '498c6f2040f241a6b01de3757ee5fc52';

    const UMENG_VERIFY_DISTRIBUT_APPKEY = '6706575061c3b13bde9e0d7a';
    const UMENG_VERIFY_DISTRIBUT_SECRETKEY = 'MDrscCkLs135R+4CccnICo8bJX9qRIADkQ8QMI+OvEb8iMLqFk7kP61YQ/Pn9YwiOJBg+HAaGgKw7jd4KfMdZvLq6wozv3yuGHR602YWzdvuRjjhciS5dBcNad+nrw8JPK8B6DoREG8wAu0xGJSHmTQYS+2r96ngwCV8YLfKqlTKo6qaNl1jLoErJ3VjZeAnBKmfbDmD1FUq+6zkQaV6zHObvHty/xxirih15LzB7TgtCsVn/xj+MCm4xa93pNR+CrZGIIeGKKfkniZ9dEjZLT9qUJq6WUeJVP5kRu8BFKe25u4x4vMrww==';


    /**
     * 友盟系统推送
     * @param Request $request
     * @return \think\Response
     */
    public function umengPushMsg(int $uid, string $order_id='', int $type=1, string $gain_integral='',$str='')
    {
        try {

            $userServices = app()->make(UserServices::class);
            $userInfo = $userServices->getUserInfo($uid);

            $umeng = new Youmen(self::UMENG_APPKEY, self::UMENG_APPSECRET);

            $str = "您的订单".$order_id . "已支付成功，请等待发货。";  //订单支付成功提醒
//            if($type == 2) $str = "您的订单".$order_id . "已收货成功。";   //确认收货提醒
//            if($type == 3) $str = "您的订单".$order_id . "还未付款，请付款。";    //提醒付款通知
//            if($type == 4) $str = "您的订单".$order_id . "获得了赠送的".$gain_integral."积分。";    //积分到账通知
            if($type == 5) $str = "分享新用户注册赠送给您".$gain_integral."积分。";    //积分到账通知
//            if($type == 6) $str = "您的卡密订单".$order_id . "已发货。";    //卡密商品发货通知
//            if($type == 7) $str = "您的订单".$order_id . "已改价成功。".$gain_integral."元";    //改价成功提醒
//            if($type == 8) $str = "您的订单".$order_id . "已发货。";    //订单发货提醒
//            if($type == 9) $str = "您的订单".$order_id . "已退款成功。退款".$gain_integral."元";    //退款成功提醒
            if($type == 10) $str = "您的新人礼注册积分已到账。新用户注册奖励".$gain_integral."积分";    //积分到账通知 - 新用户注册奖励
//            if($type == 11) $str = "你的朋友们已经帮你砍到底价了，商品名称{$order_id}，底价{$gain_integral}元";    //砍价成功提醒发送
//            if($type == 12) $str = "亲，您的拼团已经完成了，拼团名称{$order_id}，拼团人数{$gain_integral}人";    //拼团成功发送系统友盟通知
//            if($type == 13) $str = "亲，您的拼团失败，活动名称{$order_id}，拼团人数{$gain_integral}人";    //拼团失败发送系统友盟通知
//            if($type == 14) $str = "亲，您已成功参与拼团，拼团名称为{$order_id}";    //开团成功发送系统友盟通知
//            if($type == 15) $str = "亲，您成功提现佣金{$gain_integral}元";    //提现成功提醒发送
//            if($type == 16) $str = "亲，您的提现佣金{$gain_integral}元申请未通过。";    //提现失败提醒发送
//            if($type == 17) $str = "亲，您的拼团取消，活动名称{$order_id}。";    //取消拼团提醒发送
//            if($type == 18) $str = "亲，您已成功参与拼团，活动名称{$order_id}。";    //参团成功提醒发送
//            if($type == 19) $str = "您好！您的订单{$order_id}已拒绝退款。";    //退款申请未通过提醒发送
//            if($type == 20) $str = "您的积分商城订单".$order_id . "已支付成功，请等待发货。";
//            if($type == 21) $str = "您好，您{$order_id}提交的{$gain_integral}供应商入驻申请已通过。";   //入驻审核通过提醒
//            if($type == 22) $str = "您好，您{$order_id}提交的{$gain_integral}供应商入驻申请审核未通过。";  //入驻审核未通过提醒
            if($type == 23) $str = "亲，恭喜您成功获得佣金{$gain_integral}元。";   //佣金到账提醒
            if($type == 24) $str = "您成功充值￥{$order_id}，现剩余余额￥{$gain_integral}元。";   //充值成功发送系统消息
            if($type == 25) $str = "尊敬的顾客，您已成功核销商品： 商品名称：{$order_id}; 核销日期：{$gain_integral} 感谢您的支持！如有任何疑问，请联系我们的客服。";  //核销成功发送提醒
            if($type == 26) $str = "尊敬的顾客，您购买的商品{$order_id} 已过期，过期日期：{$gain_integral} 感谢您的支持！如有任何疑问，请联系我们的客服。";  //过期次卡发送提醒
            if($type == 27) $str = "您已签到成功。";   //签到成功提醒
            if($type == 28) $str = "您好，您提交的{$order_id}店铺转让申请审核未通过，原因为{$gain_integral}。";   //闲置店铺转让审核不通过发送给用户

		$ticker='通知标题';
	    $title='尚百街';

	        $res = $umeng->sendAndroidUnicast('As3D_-ho-j6HNc-7Ya1CEcWwLrVBCHBAltfkboONa_Rn',$ticker,$title,$str );  //安卓  单播


	        if(!empty($userInfo['device_token_an'])){
                //$umeng->sendAndroidBroadcast();  //安卓  广播
                $res = $umeng->sendAndroidUnicast($userInfo['device_token_an'],$ticker,$title,$str );  //安卓  单播
                //$umeng->sendAndroidFilecast();  //安卓  文件播

                $result = json_decode($res, true);
                if(isset($result['ret']) && $result['ret'] == 'SUCCESS'){
                    $this->createOneMsg($uid, $order_id, $str, $type, $userInfo['device_token_an'], 1, $result['data']['msg_id'] ?? '', $res);
                    return true;
                }else{
                    $this->createOneMsg($uid, $order_id, $str, $type, $userInfo['device_token_an'], 1, '', '');
                    return false;
                }
            }

            if(!empty($userInfo['device_token_ios'])){
                //$umeng->sendIOSBroadcast();  //ios  广播
                $res = $umeng->sendIOSUnicast($userInfo['device_token_ios'], $str);  //ios  单播
                //$umeng->sendIOSFilecast();  //ios  文件播

                $result = json_decode($res, true);
                if(isset($result['ret']) && $result['ret'] == 'SUCCESS'){
                    $this->createOneMsg($uid, $order_id, $str, $type, $userInfo['device_token_ios'], 2, $result['data']['msg_id'] ?? '', $res);
                    return true;
                }else{
                    $this->createOneMsg($uid, $order_id, $str, $type, $userInfo['device_token_ios'], 2, '', '');
                    return false;
                }
            }

            return false;
        }catch (\Throwable $e){
            //return app('json')->fail($e->getMessage());
            return false;
        }
    }



    public function createOneMsg($uid,$order_id,$str,$type,$device_token, $app_type,$msg_id,$response_text)
    {
        $add = [
            "uid"               => $uid,
            "order_id"          => $order_id ?? '',
            "content"           => $str ?? '',
            "type"              => $type ?? 1,
            "create_time"       => time(),
            "device_token"      => $device_token ?? '',
            "app_type"          => $app_type ?? 1,
            "msg_id"            => $msg_id ?? '',
            "response_text"     => $response_text ?? '',
        ];
        ConfigUmengMsg::create($add);
    }


}
